﻿using System;

namespace Model
{
    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    public sealed class TableAttribute : Attribute
    {
        /// <summary>
        /// 表名
        /// </summary>
        private readonly string _TableName = "";
        /// <summary>
        /// 主键名称
        /// </summary>
        private readonly string _PKName = "";
        ///// <summary>
        ///// 非删除数据条件
        ///// </summary>
        //private readonly string _UnDeleteWhere = "";

        public TableAttribute(string tableName)
        {
            this._TableName = tableName;
        }
        public TableAttribute(string tableName, string PKName)
        {
            this._TableName = tableName;
            this._PKName = PKName;
        }
        //public TableAttribute(string tableName, string PKName, string UnDeleteWhere)
        //{
        //    this._TableName = tableName;
        //    this._PKName = PKName;
        //    this._UnDeleteWhere = UnDeleteWhere;
        //}

        public string TableName
        {
            get { return this._TableName; }
        }
        public string PKName
        {
            get { return this._PKName; }
        }
        //public string UnDeleteWhere
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(this._UnDeleteWhere))
        //            return "IsDeleted=2";
        //        return this._UnDeleteWhere;
        //    }
        //}
    }
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public class FieldAttribute : Attribute
    {
        ///数据库的字段名称
        private readonly string _FieldName = "";
        ///数据库的字段类型
        private System.Data.SqlDbType _Type = System.Data.SqlDbType.NVarChar;
        ///是否主键
        private bool _Primarykey = false;
        ///是否自带字段
        private bool _Own = false;

        public FieldAttribute() { }
        /// <summary>
        /// 设置是否数据库表自带字段
        /// </summary>
        /// <param name="own"></param>
        public FieldAttribute(bool own)
        {
            this._Own = own;
        }
        /// <summary>
        /// 设置字段名
        /// </summary>
        /// <param name="fieldName"></param>
        public FieldAttribute(string fieldName)
        {
            this._FieldName = fieldName;
        }
        /// <summary>
        /// 设置字段名和字段数据类型
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="type"></param>
        public FieldAttribute(string fieldName, System.Data.SqlDbType type)
        {
            this._FieldName = fieldName;
            this._Type = type;
        }
        /// <summary>
        /// 设置字段名、字段数据类型和是否数据库表自带字段
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="type"></param>
        /// <param name="own"></param>
        public FieldAttribute(string fieldName, System.Data.SqlDbType type, bool own)
        {
            this._FieldName = fieldName;
            this._Type = type;
            this._Own = own;
        }
        /// <summary>
        /// 设置字段名、字段数据类型、是否数据库表自带字段和是否主键
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="type"></param>
        /// <param name="own"></param>
        /// <param name="primarykey"></param>
        public FieldAttribute(string fieldName, System.Data.SqlDbType type, bool own, bool primarykey)
        {
            this._FieldName = fieldName;
            this._Type = type;
            this._Own = own;
            this._Primarykey = primarykey;
        }

        public string FieldName
        {
            get { return this._FieldName; }
        }

        public System.Data.SqlDbType Type
        {
            get { return this._Type; }
        }
        public bool Primarykey
        {
            get { return this._Primarykey; }
        }
        public bool Own
        {
            get { return this._Own; }
        }
        /// <summary>
        /// 获取实体表名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetTableName<T>()
        {
            Type type = typeof(T);
            object[] objs = type.GetCustomAttributes(typeof(TableAttribute), true);
            if (objs.Length <= 0)
            {
                throw new Exception("实体类没有标识TableAttribute属性");
            }
            else
            {
                object obj = objs[0];
                TableAttribute ta = (TableAttribute)obj;
                return ta.TableName;//获取表名称
            }
        }
        /// <summary>
        /// 获取实体字段名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="m"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static string GetFieldName<T>(string propertyName)
        {
            //FieldAttribute fieldAttribute = m.GetFieldAttribute(propertyName);
            System.Reflection.PropertyInfo field = typeof(T).GetProperty(propertyName);
            if (field == null)
            {
                throw new Exception("属性名" + propertyName + "不存在");
            }
            object[] objs = field.GetCustomAttributes(typeof(FieldAttribute), true);
            if (objs.Length <= 0)
            {
                throw new Exception("类体属性名" + propertyName + "没有标识FieldAttribute属性");
            }
            else
            {
                object obj = objs[0];
                FieldAttribute fieldAttribute = (FieldAttribute)obj;
                //fieldAttribute.FieldValue = field.GetValue(this, null);
                //return fieldAttribute;
                return fieldAttribute.FieldName;
            }
        }
        /// <summary>
        /// 获取实体字段名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="m"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static FieldAttribute GetFieldAttribute<T>(string propertyName)
        {
            System.Reflection.PropertyInfo field = typeof(T).GetProperty(propertyName);
            if (field == null)
            {
                throw new Exception("属性名" + propertyName + "不存在");
            }
            object[] objs = field.GetCustomAttributes(typeof(FieldAttribute), true);
            if (objs.Length <= 0)
            {
                throw new Exception("类体属性名" + propertyName + "没有标识FieldAttribute属性");
            }
            else
            {
                object obj = objs[0];
                FieldAttribute fieldAttribute = (FieldAttribute)obj;
                //fieldAttribute.FieldValue = field.GetValue(this, null);
                //return fieldAttribute;
                return fieldAttribute;
            }
        }
    }
}
